'use strict';

const Controller = require('egg').Controller;

class LogoutController extends Controller {
  // 退出登录
  async index() {
    const { ctx, app } = this;
    const { userId = '' } = ctx.request.body
    const transaction = await app.model.transaction()
    const ExsitedList = await app.model.AdminUser.findOne({
      'attributes': ['id'],
      where: {
        id: userId
      }
    })
    if (ExsitedList.id) {
        // 更新该用户在表中的sessionId
        ctx.session.userInfo = {
          username: '',
          account: '',
          wechat: '',
          QQ: '',
          github: '',
          keywords: '',
          description: '',
          sendword: '',
          userId: ''
        }
        await app.model.AdminUser.update({session_id: ''}, {
          where: { id: userId }
        }, {transaction})
        ctx.body = { success: true, msg: '退出登录成功' };
    } else {
        ctx.body = { success: false, msg: '退出登录失败' };
    }
  }
}

module.exports = LogoutController;